package com.xinxin.datastructure.linkedlist;

/**
 * @author ：史鑫鑫
 * @date ：Created in 2019/4/23 19:53
 */
public class LinkedList<E> {
    // 节点内部类
    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    private Node dummyHhead;
    private Node tail;
    private int size;

    /// 构造函数，创建虚拟头结点
    public LinkedList() {
        dummyHhead = new Node();
        tail = dummyHhead;
        size = 0;
    }

    // 获取链表中元素的个数
    public int getSize() {
        return size;
    }

    // 返回链表是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = dummyHhead.next;
        while (cur != null) {
            res.append(cur.e);
            res.append("->");
            cur = cur.next;
        }
        res.append("NULL");
        return res.toString();
    }

    // 指定位置添加元素
    public void add(int index, E e) {
        if (index < 0 | index > size) {
            throw new IllegalArgumentException("Add failed. Illegal index.");
        }
        if (index == size++) {
            tail.next = new Node(e, null);
            tail = tail.next;
            return;
        }
        Node prev = dummyHhead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        prev.next = new Node(e, prev.next);
    }

    // 在链表头添加元素
    public void addFirst(E e) {
        add(0, e);
    }

    // 在链表尾部添加元素
    public void addLast(E e) {
        add(size, e);
    }

    // 获取链表的第index个位置的元素
    public E get(int index) {
        if (index < 0 | index >= size) {
            throw new IllegalArgumentException("Get failed. Illegal index.");
        }
        if (index == size - 1) {
            return tail.e;
        }
        Node currentNode = dummyHhead.next;
        for (int i = 0; i < index; i++) {
            currentNode = currentNode.next;
        }
        return currentNode.e;
    }

    // 获取链表的第一个元素
    public E getFirst() {
        return get(0);
    }

    // 获取链表的最后一个元素
    public E getLast() {
        return get(size - 1);
    }

    // 修改链表的第index个位置的元素为e
    public void set(int index, E e) {
        if (index < 0 | index >= size) {
            throw new IllegalArgumentException("Update failed. Illegal index.");
        }
        if (index == size - 1) {
            tail.e = e;
            return;
        }
        Node currentNode = dummyHhead.next;
        for (int i = 0; i < index; i++) {
            currentNode = currentNode.next;
        }
        currentNode.e = e;
    }

    // 查找链表中是否有元素e
    public boolean contains(E e) {
        Node currentNode = dummyHhead.next;
        while (currentNode != null) {
            if (e.equals(currentNode.e))
                return true;
            currentNode = currentNode.next;
        }
        return false;
    }

    // 在链表中删除元素，返回删除元素
    public E remove(int index) {
        if (index < 0 | index >= size) {
            throw new IllegalArgumentException("Add failed. Illegal index.");
        }
        Node prev = dummyHhead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        if (index == size - 1) {
            tail = prev;
        }
        Node retNode = prev.next;
        prev.next = retNode.next;
        retNode.next = null;
        size--;
        return retNode.e;
    }

    // 删除链表中的第一个元素
    public E removeFirst() {
        return remove(0);
    }

    // 删除链表的最后一个元素
    public E removeLast() {
        return remove(size - 1);
    }

    // 删除指定元素
    public boolean removeElement(E e) {
        Node cur = dummyHhead;
        while (cur != null) {
            if (e.equals(cur.next.e)) {
                Node ret = cur.next;
                cur.next = ret.next;
                ret.next = null;
                size--;
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
}
